//
//  SNewsViewController.m
//  News
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 PEAK. All rights reserved.
//

#import "SNewsViewController.h"

@interface SNewsViewController ()< UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic ,retain)UIScrollView * scrollView;
@property(nonatomic ,retain)NSArray *arr;
@property(nonatomic ,retain)UIButton *Bu;
@property(nonatomic ,assign)NSInteger i;
@property(nonatomic ,retain)UICollectionView *collection;
@property(nonatomic ,retain)NSMutableArray * MutableArrOne;
@property(nonatomic ,retain)NSMutableArray * MutableArrTwo;
@property(nonatomic ,retain)NSString *TouTiaoStrOne;
@property(nonatomic ,assign)NSInteger  integer;
@property(nonatomic ,retain)UIActivityIndicatorView * ActivityIndicatorViewOne;
@property(nonatomic ,assign)NSInteger Six;



@end

@implementation SNewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    /**
     *  顶部预留
     */
    self.automaticallyAdjustsScrollViewInsets = NO;
      self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"随心闻";

            [NetworkHandle startNetMonitoringWithCompletion:^(id result) {
    
                NSString *str = [result objectForKey:@"info"];
                if ([str isEqualToString:@"无网络,请检查手机能否上网!"]) {
                    UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                 
                    [al show];
                }
    
            }];
    [self createNotificationCenter];

    /**
     *  获取数据
     */
    [self getData];
   [self createView];
    self.navigationController.navigationBar.titleTextAttributes = @{UITextAttributeTextColor: [UIColor whiteColor], UITextAttributeFont:[UIFont boldSystemFontOfSize:20]};
  
}

/**
 *  视图创建方法
 */
-(void)createView
{
    

    /**
创建UIView
     */
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 13.5)];

    /**
     创建滑动标题视图
     */
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, view1.frame.size.width, view1.frame.size.height)];
    _scrollView.backgroundColor = [UIColor whiteColor];
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 1.4, 0);
    _scrollView.showsHorizontalScrollIndicator = NO;
    /**
     *  创建标题数组
     */
    self.arr = @[@"头条", @"社会", @"历史", @"军事", @"科技", @"读书",@"时尚"];
    
    /**
     *  创建按钮
     */
    for (NSString *temp in _arr){
        
        self.Bu = [UIButton buttonWithType:UIButtonTypeSystem];
        _Bu.frame = CGRectMake(_i * _scrollView.frame.size.width / 4.6, _scrollView.frame.size.height / 10, _scrollView.frame.size.width / 9.4, _scrollView.frame.size.height - _Bu.frame.size.height);
        _Bu.tintColor = [UIColor grayColor];
        [_Bu setTitle:temp forState:UIControlStateNormal];
        _Bu.tag = 1000 + _i;
        [_Bu addTarget:self action:@selector(ButtonWay:) forControlEvents:UIControlEventTouchUpInside];
        [_scrollView addSubview:_Bu];
        
        _i++;


   }
    /**
     *  设置初始按钮颜色
     *
     */
    UIButton *bu = (UIButton *)[_scrollView viewWithTag:1000];
    bu.tintColor = [UIColor redColor];
  
    /**
     *  添加视图
     */
    [view1 addSubview:_scrollView];
    [self.view addSubview:view1];
    
    [_scrollView release];
    [view1 release];
    
    /**
     创建collectionView样式
     */
    UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc] init];
    
    //    设置item大小
    flow.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - ( view1.frame.size.height));
   flow.minimumLineSpacing = 0;
    //    方向
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    /**
     创建collectionView
     */
   self.collection  = [[UICollectionView alloc] initWithFrame:CGRectMake(0, view1.frame.size.height , self.view.frame.size.width, self.view.frame.size.height - ( view1.frame.size.height)) collectionViewLayout:flow];
    _collection.dataSource = self;
    _collection.delegate = self;
    
    //    添加滑动效果
    _collection.pagingEnabled = YES;
    _collection.scrollEnabled = YES;
    
    //    添加
    [self.view addSubview:_collection];
    [_collection release];
    [flow release];
    
    //    注册cell
    [_collection registerClass:[SNewsMyCollectionViewCell class] forCellWithReuseIdentifier:@"123"];
    
}
/**
 *  按钮方法
 *
 *  @param Bu1 当前按钮
 */
-(void)ButtonWay:(UIButton *)Bu1
{
     /**
     *  按钮传回来的值
     */
   self.integer  = Bu1.tag - 1000;
    /**
     *  创建path
     */

    NSIndexPath * path = [NSIndexPath indexPathForItem:_integer inSection: 0 ];

    /**
     *  跳转方法
     */
    [_collection scrollToItemAtIndexPath: path atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];

}


/**
 *  获取数据
 */
-(void)getData
{
    
    self.TouTiaoStrOne = [[NSString alloc] init];
    
    [SNetWorkHandle getDataWithURL:@"http://c.m.163.com/nc/topicset/ios/subscribe/manage/listspecial.html" completion:^(id result) {
        
        
        /**
         *  所有名字数组 (头条 原创 娱乐等)
         */
        NSArray *ArrOne = [result objectForKey:@"tList"];
        self.MutableArrOne = [NSMutableArray array];
        self.MutableArrTwo = [NSMutableArray array];
        

        for (NSDictionary   * temp in ArrOne) {
            NSString *strOne = [temp objectForKey:@"tname"];

            if ([strOne isEqualToString:@"头条"]) {
                NSDictionary * dicOne = temp;
                NSString * TouTiao = [dicOne objectForKey:@"tid"];
                [_MutableArrTwo addObject:strOne];
                [_MutableArrOne addObject:TouTiao];
                
            }
            
            else if ([strOne isEqualToString:@"历史"]) {
                NSDictionary * dicOne = temp;
                NSString * LiShi = [dicOne objectForKey:@"tid"];
               [_MutableArrTwo addObject:strOne];
                [_MutableArrOne addObject:LiShi];

            }
            else if ([strOne isEqualToString:@"社会"]) {
                NSDictionary * dicOne = temp;
                NSString * SheHui = [dicOne objectForKey:@"tid"];
                
              [_MutableArrTwo addObject:strOne];
                [_MutableArrOne addObject:SheHui];
                
            }
            else  if ([strOne isEqualToString:@"军事"]) {
                NSDictionary * dicOne = temp;
                NSString * JunShi = [dicOne objectForKey:@"tid"];
                [_MutableArrTwo addObject:strOne];
                [_MutableArrOne addObject:JunShi];
              

            }
            else  if ([strOne isEqualToString:@"时尚"]) {
                NSDictionary * dicOne = temp;
                NSString *  ShiShang = [dicOne objectForKey:@"tid"];
                [_MutableArrTwo addObject:strOne];
                [_MutableArrOne addObject:ShiShang];
               
                
            }
            else  if ([strOne isEqualToString:@"科技"]) {
                NSDictionary * dicOne = temp;
                NSString *  KeJi = [dicOne objectForKey:@"tid"];
               [_MutableArrTwo addObject:strOne];
                [_MutableArrOne addObject:KeJi];
                
            }
            else  if ([strOne isEqualToString:@"读书"]) {
                NSDictionary * dicOne = temp;
                NSString * DuShu = [dicOne objectForKey:@"tid"];
             [_MutableArrTwo addObject:strOne];
                [_MutableArrOne addObject:DuShu];
                
            }
            
        }
        /**
         *  视图创建
         */
      
        [_collection reloadData];
    }];
    
    
}


//实现协议方法

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arr.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SNewsMyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"123" forIndexPath:indexPath];

    
    /**
     *  给自定义cell页面视图控制器属性赋值
     */
    cell.ViewController = self;
    

    cell.TouTiao = _MutableArrOne[indexPath.item];


    

   
    
    
    return cell;
}
-(void)createNotificationCenter
{

    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];

    [center addObserver:self selector:@selector(changColor:) name:@"color" object:nil];
    
    
}

-(void)changColor:(NSNotification *)noti
{
    [self getData];

}


/**
 *  滑动方法
 *
 *  @param scrollView 当前滑动视图
 */
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if (scrollView == _collection) {
        
        /**
         *  获取collectionView 的偏移量
         */
        CGPoint point = _collection.contentOffset;

        NSInteger a = (NSInteger)(point.x)/self.view.frame.size.width;

        /**
         *  改变按钮文字颜色
         */
        UIButton *bu = (UIButton *) [_scrollView viewWithTag:1000 + a];
        bu.tintColor = [UIColor redColor];
        
        UIButton *bu1 = (UIButton *) [_scrollView viewWithTag:1000 + a - 1];
        bu1.tintColor = [UIColor grayColor];
        
        UIButton *bu2 = (UIButton *) [_scrollView viewWithTag:1000 + a + 1];
        bu2.tintColor = [UIColor grayColor];
        
        [_scrollView setContentOffset:CGPointMake(30 * a, 0) animated:YES];
     
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
